package leeon.mobile.server.bbscache;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import leeon.mobile.BBSBrowser.models.BlockObject;

import com.google.appengine.api.utils.SystemProperty;

/**
 * 将缓存的信息渲染的servlet
 * @author leeon
 */
@SuppressWarnings("serial")
public class BbsviewServlet extends HttpServlet {
	
	private BBSViewService service = new BBSViewService();
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String url = forwardUrl(req);
		if (url == null) doit(req, resp);
		else forward(url, req, resp);
	}
	
	private String forwardUrl(HttpServletRequest req) {
		if (SystemProperty.environment.value() == 
				SystemProperty.Environment.Value.Development)
			return null;
		
		if (!SystemProperty.applicationId.get().equals("fdubbsapp"))
			return null;
		
		String boardName = req.getParameter("board");
		String bid = req.getParameter("bid");
		if (boardName == null && bid == null) return null;
		BlockObject b = Utils.findBoardInBlock(boardName, bid==null?null:Long.valueOf(bid));
		
		String url = BBSBoardService.createViewForwardUrl(b, boardName, bid);
		if (url == null) return null;
		
		url += req.getRequestURI();
		url += "?" + req.getQueryString();
		return url;
	}
	
	private void forward(String url, HttpServletRequest req, HttpServletResponse resp) throws IOException {
		InputStream is = Utils.readUrl(url, resp);		
		resp.setBufferSize(1024);
		OutputStream os = resp.getOutputStream();
		try {
			byte[] b = new byte[1024];
			int i = 0;
			while ((i = is.read(b)) != -1) {
				os.write(b, 0, i);
			}
		} finally {
			os.flush();
			os.close();
			is.close();
		}
	}
	
	private void doit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String boardName = req.getParameter("board");
		String start = req.getParameter("start");
		String bid = req.getParameter("bid");
		String fid = req.getParameter("f");
		String gid = req.getParameter("g");
		String a = req.getParameter("a");
		String s = req.getParameter("s");
		boolean n = "1".equals(req.getParameter("new"));

		String pathInfo = req.getPathInfo();
		String xml = null;
		if ("/sec".equals(pathInfo)) {
			xml = service.sec();
		} else if ("/all".equals(pathInfo)) {
			xml = service.all();
		} else if ("/top10".equals(pathInfo)) {
			xml = service.top10();
		} else if ("/boa".equals(pathInfo)) {
			xml = service.boa(boardName, s);
		} else if ("/doc".equals(pathInfo)) {
			xml = service.doc(boardName, bid!=null?Long.valueOf(bid):null, start!=null?Integer.parseInt(start):0);
		} else if ("/tdoc".equals(pathInfo)) {
			xml = service.tdoc(boardName, bid!=null?Long.valueOf(bid):null, start!=null?Integer.parseInt(start):0);
		} else if ("/con".equals(pathInfo)) {
			xml = service.con(boardName, bid!=null?Long.valueOf(bid):null, Long.valueOf(fid), a, n);
		} else if ("/tcon".equals(pathInfo)) {
			xml = service.tcon(boardName, bid!=null?Long.valueOf(bid):null, 
				Long.valueOf(fid), gid!=null?Long.valueOf(gid):null, a, n);			
		}
		
		if (xml != null) {
			resp.setContentType("text/xml");
			resp.setCharacterEncoding("GB18030");
			
			resp.getWriter().write("<?xml version=\"1.0\" encoding=\"gb18030\"?>\n");
			resp.getWriter().write("<?xml-stylesheet type=\"text/xsl\" href=\"../xsl/bbs.xsl\"?>\n");
			resp.getWriter().write(xml);
		} else {
			resp.setContentType("text/plain");
			resp.setCharacterEncoding("GB18030");
			
			resp.getWriter().write("参数错误");
		}
	}

}
